*********************************************** 
** To construct index of court decisions on  **
** patent cases before/after Federal Circuit **
*********************************************** 

set scheme burd

use "CAFC_cases_proed.dta" , clear

*Sample of litigation data: 15 years before 1983 and 15 years after 1985 
keep if year >= 1968 & year <= 2000
drop if mi(dccircuit)
drop if mi(subcat) 
rename dccircuit circuit

gen outcome = 0 if (invdc == 1 & appeal == 0) | (invac == 1 & appeal == 1)
replace outcome = 1 if (ninfdc == 1 & appeal == 0) | (ninfac == 1 & appeal == 1)
replace outcome = 2 if (validdc == 1 & appeal == 0) | (validac == 1 & appeal == 1)
drop if mi(outcome)

gen cafcera_lab = "Before CAFC" if cafcera == 0
replace cafcera_lab = "Post CAFC" if cafcera == 1
gen outcome_lab = "Invalid" if outcome == 0
replace outcome_lab = "Not infringing" if outcome == 1
replace outcome_lab = "Valid and infringed" if outcome == 2

** Home circuit : Inventor cases; Assignee cases **
gen home_circuit = 1 if asscircuit1 == circuit  
replace home_circuit = 1 if asscircuit2 == circuit & !mi(asscircuit2)
replace home_circuit = 1 if invcircuit == circuit &  ///
 !mi(invcircuit) & mi(home_circuit)
replace home_circuit = 0 if mi(home_circuit)

gen hmcircuit = asscircuit1
replace hmcircuit = asscircuit2 if mi(hmcircuit)
replace hmcircuit =  invcircuit if mi(hmcircuit)

**Making a table for forum shopping distribution** 
tab circuit hmcircuit if home_circuit == 0 & cafcera == 0
tab circuit hmcircuit if home_circuit == 0 & cafcera == 1
gen byte ass = cond(assignee == "none", 0 , 1)
 
gen home = asscircuit1
replace home = invcircuit if mi(asscircuit1)
forvalues i = 1/11{
  gen byte homect`i' = 0 
  replace homect`i' = 1 if home == `i' 
  label variable homect`i' "Home circuit as `i'"
  }
  
forvalues i = 1/11 {
  gen byte homecafcct`i' = 0
  replace homecafcct`i' = 1 if home == `i' & cafcera == 1
  label variable homecafcct`i' "Home circuit as `i', post-CAFC"  
  } 
  
label var ass "Business applicant"
label var claims "Number of claims"
label var appeal "Appeal"
label var numinventors "Number of inventors"
label var patdef "Patentee as the defendant"

** Create index of patent validity by circuit **
forvalues i = 1/11{
  gen byte ct`i' = 0 
  replace ct`i' = 1 if circuit == `i' 
  label variable ct`i' "Circuit `i'"
  }
  
forvalues i = 1/11 {
  gen byte cafcct`i' = 0
  replace cafcct`i' = 1 if circuit == `i' & cafcera == 1
  label variable cafcct`i' "Post-CAFC Circuit `i'"  
  } 

drop if circuit == 11 & cafcera == 0
drop if circuit == 12  /// (DC, only 9 cases)
 
egen ct_post = group(cafcera circuit)

global outopt landscape plain coljust(lc) var se starloc(1) ///
  starlevels(10 5 1)  bdec(3) summstat(N) ///
  summtitles(Observations)
 

* Estimates *
gen ratepre_all = . 
gen ratepost_all = . 
label var ratepre_all "All cases"
label var ratepost_all "All cases"
gen ratepre_home = .
gen ratepost_home = . 
label var ratepre_home "Home circuit" 
label var ratepost_home "Home circuit" 
gen ratepre_pattiff = .
gen ratepost_pattiff = . 
label var ratepre_pattiff "Patent owner as plaintiff"
label var ratepost_pattiff "Patent owner as plaintiff"
gen ratepre_home_pattiff = .
gen ratepost_home_pattiff = .
label var ratepre_pattiff "Patent owner as plaintiff at home circuit"
label var ratepost_pattiff "Patent owner as plaintiff at home circuit"
 
global sample1 if !mi(subcat) & !mi(circuit) 
 ///preferred 
global sample2 if !mi(subcat) & !mi(circuit) & patdef == 0 
global sample3 if !mi(subcat) & !mi(circuit) & home_circuit == 1
global sample4 if !mi(subcat) & !mi(circuit) & home_circuit == 1 & patdef == 0

save "CAFC_reg.dta", replace 

** Sample 1: All cases **
xi: ologit outcome patdef claims ass home_circuit appeal  ///
  ct1 ct2 ct4-ct10 cafcct1-cafcct11 hjt1-hjt5 i.year ///
   $sample1, robust
qui regsave using "CAFC_regsave.dta",  pval ///
 table(CAFC, parentheses(stderr) brackets(tstat) format(%8.3f) ) replace
est store sample1 
 
gen sepre_all = .
gen sepost_all = .
gen ppost_all = . 
gen ppre_all = .

xi: ologit outcome patdef claims ass home_circuit appeal  ///
  ct1 ct2 ct4-ct10 cafcct1-cafcct11 hjt1-hjt5 i.year ///
   $sample1, robust

foreach j in 1 2 4 5 6 7 8 9 10{
  replace ratepre_all = _b[ct`j'] if circuit == `j'
  replace sepre_all = _se[ct`j']  if circuit == `j'
  quietly lincom ct`j'
  replace ppre_all = `r(p)'
  quietly lincom ct`j' +   cafcct`j'   
  replace ratepost_all = `r(estimate)' if circuit == `j' 
  replace sepost_all = `r(se)'   if circuit == `j' 
  replace ppost_all = `r(p)' if circuit == `j'
}

replace ratepre_all = 0 if circuit == 3
replace ratepost_all = _b[cafcct3] if circuit == 3
replace ratepre_all = _b[ct5] if circuit == 11
replace ratepost_all = _b[cafcct11] if circuit == 11

replace sepost_all = _se[cafcct3] if circuit == 3
lincom cafcct3 
replace ppost_all = `r(p)' if circuit == 3
replace sepost_all = _se[cafcct11] if circuit == 11
lincom cafcct11 
replace ppost_all = `r(p)' if circuit == 11

lincom cafcct11 - ct5 

test ct1 + cafcct1 = ct2 + cafcct2 = cafcct3 = ct4 + cafcct4 = ct5 + cafcct5 ///
 = ct6 + cafcct6 = ct7 + cafcct7 = ct8 + cafcct8 = ct9 + cafcct9 = ct10 + cafcct10 ///
 = cafcct11 
 
**Sample 2: Cases that are limited to patent plaintiff 
xi: ologit outcome claims ass home_circuit appeal  ///
  ct1 ct2 ct4-ct10 cafcct1-cafcct11 hjt1-hjt5 i.year ///
   $sample2, robust
qui regsave using "CAFC_sample2",  pval ///
 table(CAFC, parentheses(stderr) brackets(tstat) format(%8.3f) ) replace
est store sample2 
 
foreach j in 1 2 4 5 6 7 8 9 10{
  replace ratepre_pattiff = _b[ct`j'] if circuit == `j'
  quietly lincom ct`j' +   cafcct`j'   
  replace ratepost_pattiff = `r(estimate)' if circuit == `j' 
}
replace ratepre_pattiff = 0 if circuit == 3
replace ratepost_pattiff = _b[cafcct3] if circuit == 3
replace ratepre_pattiff = _b[ct5] if circuit == 11
replace ratepost_pattiff = _b[cafcct11] if circuit == 11

**Sample 3: Cases that are limited to home circuit only 
xi: ologit outcome patdef claims ass home_circuit appeal  ///
  ct1 ct2 ct4-ct10 cafcct1-cafcct11 hjt1-hjt5 i.year ///
   $sample3, robust
qui regsave using "CAFC_sample3",  pval ///
 table(CAFC, parentheses(stderr) brackets(tstat) format(%8.3f) ) replace
est store sample3
 
foreach j in 1 2 4 5 6 7 8 9 10{
  replace ratepre_home = _b[ct`j'] if circuit == `j'
  quietly lincom ct`j' +   cafcct`j'   
  replace ratepost_home = `r(estimate)' if circuit == `j' 
}
replace ratepre_home = 0 if circuit == 3
replace ratepost_home = _b[cafcct3] if circuit == 3
replace ratepre_home = _b[ct5] if circuit == 11
replace ratepost_home = _b[cafcct11] if circuit == 11

**Sample 4: Cases that are limited to home circuit and patentee as the plaitiff only  
xi: ologit outcome claims ass home_circuit appeal  ///
  ct1 ct2 ct4-ct10 cafcct1-cafcct11 hjt1-hjt5 i.year ///
   $sample4, robust
qui regsave using "CAFC_sample4",  pval ///
 table(CAFC, parentheses(stderr) brackets(tstat) format(%8.3f) ) replace
est store sample4
 
foreach j in 1 2 4 5 6 7 8 9 10{
  replace ratepre_home_pattiff = _b[ct`j'] if circuit == `j'
  quietly lincom ct`j' +   cafcct`j'   
  replace ratepost_home_pattiff = `r(estimate)' if circuit == `j' 
}
replace ratepre_home_pattiff = 0 if circuit == 3
replace ratepost_home_pattiff = _b[cafcct3] if circuit == 3
replace ratepre_home_pattiff = _b[ct5] if circuit == 11
replace ratepost_home_pattiff = _b[cafcct11] if circuit == 11

keep circuit rate*  se*   ppre* ppost*

foreach i in all home pattiff home_pattif{
 gen gap_`i' = ratepost_`i' - ratepre_`i'
}
duplicates drop 
compress

save "CAFC_ct_updated.dta", replace 

import excel using "hm_dc_circuit.xlsx", clear first
drop ratepre_all_1-ratepost_all_11
save  "hm_dc_circuit.dta", replace 

import excel using "hm_dc_circuit.xlsx", clear first

forvalues i = 1/10{
 gen temppre`i' = ratepre_all_`i' * pre`i'
}
gen temppre11 = temppre5 
egen ratepre_all_wt = rowtotal(temppre1 temppre2 temppre3 temppre4 temppre5 temppre6 ///
 temppre7 temppre8 temppre9 temppre10 temppre11)
drop temppre* 

forvalues i = 1/11{
 gen temppost`i' = ratepost_all_`i' * post`i'
}
egen ratepost_all_wt = rowtotal(temppost1 temppost2 temppost3 temppost4 temppost5 ///
 temppost6 temppost7 temppost8 temppost9 temppost10 temppost11)
drop temppost*
drop pre* post*
keep circuit ratepre_all_wt ratepost_all_wt
replace ratepre_all_wt = 1.768984 if circuit == 11

save "hm_dc_circuit.dta", replace 

merge 1:1 circuit using "CAFC_ct_updated.dta"
drop _merge 
save "CAFC_ct_updated.dta", replace 

twoway scatter gap_all gap_home gap_pattiff gap_home_pattif circuit, ///
 xlab(1 2 3 4 5 6 7 8 9 10 11) ytitle("Pro-patentee change due to the CAFC") ///
 xtitle(Circuits) legend(pos(6) order(1 "All cases" 2 "Home circuit" ///
 3 "Patentee as plaintiff"  4 "Home circuit with patentee as plaintiff"))
graph export "CAFC_construction.png", as(png) replace

